/**
 * @class   wosh::gui::BuildingObjectGraphicsItem
 * @brief   Custom QGraphicsItem for drawing and managing wosh::building::BuildingObject
 *          
 *          
 *
 ****************************************************************************
 * @version $Id: BuildingObjectGraphicsItem.h 2887 2010-08-11 08:09:41Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Gui_BuildingObjectGraphicsItem_H__
 #define __WOSH_Gui_BuildingObjectGraphicsItem_H__

 #include <framework/building/BuildingObject.h>
 #include <interfaces/services/BuildingManager.h>
 #include <framework/gui/IWidget.h>
 #include <framework/gui/WoshObject.h>

 #include <QtGui/QGraphicsItem>
 #include <QtGui/QGraphicsPolygonItem>
 #include <QDateTime>
 #include <QTimer>

 #include <QtGui/QStyleOptionGraphicsItem>
 #include <QtGui/QPainter>


namespace wosh {
 namespace gui {

class BuildingObjectGraphicsItem;

class IBuildingObjectGraphicsItemListener
 {
	public:
		virtual void item_event( Message* message, BuildingObjectGraphicsItem* source ) = 0;

	public:
		virtual ~IBuildingObjectGraphicsItemListener() { }

};


class BuildingObjectGraphicsItem :  public QGraphicsPolygonItem, public WoshObject  {

	public:

		BuildingObjectGraphicsItem( IBuildingObjectGraphicsItemListener* parent = NULL );

		virtual ~BuildingObjectGraphicsItem();

		static BuildingObjectGraphicsItem* createItemOf( const std::string& itemType, IBuildingObjectGraphicsItemListener* listener = NULL );

		WRESULT configureFor( const wosh::building::BuildingObject* bobj );

	public:

		virtual void setParent( BuildingObjectGraphicsItem* new_parent, bool fix_refs ) {
			WoshObject::setParent(new_parent, fix_refs);
			if ( new_parent == NULL )
				this->bObjParent = "";
			else {
				this->bObjParent = new_parent->getRemoteName();
				//this->setZValue( new_parent->zindex()+1 );
			 }
		 }

		std::string& getBObjParent()					{ return this->bObjParent; }
		const std::string& getBObjParent() const		{ return this->bObjParent; }


		virtual void remoteObject_updated();

		
		virtual void parseMessage( const Message& message );

		virtual bool setProperty( const std::string& key, const Variant& value );
		virtual Variant* getProperty( const std::string& key ) const;

	protected:
		virtual void drawUpdate();


	protected:
		virtual bool raiseMessage( Message* message );
		virtual bool raiseMessage( MessageContent* content );



	protected:
	//	void mousePressEvent(QGraphicsSceneMouseEvent *event);
	//	void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
	//	void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
	//	virtual void hoverEnterEvent ( QGraphicsSceneHoverEvent * event )
	//	virtual void hoverLeaveEvent ( QGraphicsSceneHoverEvent * event )

		virtual void advance ( int phase );

		virtual void contextMenuEvent( QGraphicsSceneContextMenuEvent *event );
		virtual QVariant itemChange( GraphicsItemChange change, const QVariant &value );


		virtual void buildContextMenu();
		virtual void updateContextMenu();
		virtual void contextMenuAction( QAction *selectedAction );


	public slots:
		void blink( bool on );

	public:
		std::string bObjParent;
		QStringList bObjInterfaces;

		double height;

	protected:
		IBuildingObjectGraphicsItemListener* listener;

		QDateTime updated;

		long blinkingTs;
		bool blinking;
		QBrush defBrush;
		QBrush blinkBrush;

		QMenu* contextMenu;
		QAction* objectAction;
		QMenu* contextMenuChildren;
		QAction* editAction;
		QAction* propertiesAction;
		QAction* methodsAction;
		QAction* visibleAction;
		QAction* moveableAction;
		QAction* moveToFrontAction;
		QAction* moveToBackAction;

}; // class def

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

static const int imageRTTI = 984376;

class BuildingObjectGraphicsItemAudio :  public BuildingObjectGraphicsItem  {

	public:

		BuildingObjectGraphicsItemAudio( IBuildingObjectGraphicsItemListener* parent = NULL );

		virtual ~BuildingObjectGraphicsItemAudio() { }

	public:

//		virtual bool parseFact( const Fact* fact);
//		virtual bool setProperty( const string& key, const Variant& value );
//		virtual Data* getProperty( const string& key ) const;



	public:
		int rtti () const { return imageRTTI; }

		virtual void setPolygon( const QPolygonF& polygon );

	protected:

		virtual void drawUpdate() { }

		virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *);

		virtual void buildContextMenu();
		virtual void updateContextMenu();
		virtual void contextMenuAction( QAction *selectedAction );


	protected:
		QPixmap pixmap;

	protected:

		QMenu* contextMenuSynch;
		QAction* playAction;
		QAction* stopAction;
		QAction* muteAction;
		QAction* volumeUpAction;
		QAction* volumeDownAction;

}; // class def

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

class BuildingObjectGraphicsItemLight :  public BuildingObjectGraphicsItem  {

	public:
		BuildingObjectGraphicsItemLight( IBuildingObjectGraphicsItemListener* listener = NULL );
		virtual ~BuildingObjectGraphicsItemLight() { }

	public:

	protected:
		virtual void parseMessage( const Message& message );

		virtual bool setProperty( const std::string& key, const Variant& value );

		virtual void drawUpdate();
		virtual void buildContextMenu();
		virtual void updateContextMenu();
		virtual void contextMenuAction( QAction *selectedAction );

	protected:
		void turned( short onoff );
		short state;

	protected:
		QAction* turnOnAction;
		QAction* turnOffAction;
		QAction* dimAction;
		QAction* brightAction;

}; // class def

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

class BuildingObjectGraphicsItemDoor :  public BuildingObjectGraphicsItem  {

	public:
		BuildingObjectGraphicsItemDoor( IBuildingObjectGraphicsItemListener* listener = NULL );
		virtual ~BuildingObjectGraphicsItemDoor() { }

	public:

	protected:
		virtual void parseMessage( const Message& message );

		virtual bool setProperty( const std::string& key, const Variant& value );

		virtual void drawUpdate();
		virtual void buildContextMenu();
		virtual void updateContextMenu();
		virtual void contextMenuAction( QAction *selectedAction );

	protected:
		void opened( short onoff );
		short state;

	protected:
		QAction* openAction;

}; // class def

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

class BuildingObjectGraphicsItemWindow :  public BuildingObjectGraphicsItem  {

	public:
		BuildingObjectGraphicsItemWindow( IBuildingObjectGraphicsItemListener* listener = NULL );
		virtual ~BuildingObjectGraphicsItemWindow() { }

	protected:
		virtual void parseMessage( const Message& message );

		virtual bool setProperty( const std::string& key, const Variant& value );

		virtual void drawUpdate();
		virtual void buildContextMenu();
		virtual void updateContextMenu();
		virtual void contextMenuAction( QAction *selectedAction );

	protected:
		void opened( short onoff );
		short state;

	protected:
		QAction* slideUpAction;
		QAction* slideDownAction;

}; // class def

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void parseString( const QString& verStr, double& x, double& y );


 }; // namespace gui
}; // namespace wosh


#endif // __WOSH_Gui_BuildingObjectGraphicsItem_H__
